
// #ifndef VUE3
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import lang from 'element-ui/lib/locale/lang/en'
import locale from 'element-ui/lib/locale'
import 'element-ui/lib/theme-chalk/index.css'
import { getToken } from '@/utils/auth'

Vue.config.productionTip = false
locale.use(lang)
Vue.use(ElementUI)

// API
import api from './api'
Vue.use(api)

// Clipboard
import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard)

import md5 from 'js-md5'
Vue.prototype.$md5 = md5
const Base64 = require('js-base64').Base64

import FBSignInButton from 'vue-facebook-signin-button'
Vue.use(FBSignInButton)

import cookie from 'js-cookie'

import VueI18n from 'vue-i18n'
Vue.use(VueI18n)

App.mpType = 'app'

// Obtain language packs and import them
// const req = require.context('./static/language', false, /\.js$/)
// const requireAll = (requireContext) => requireContext.keys()
// const re = /\.\/(.*)\.js/
// const messages = {
//   EN: require('./static/language/EN')
// }
// let languageNames = []
// requireAll(req).forEach((i) => {
//   const filename = i.match(re)[1]
//   languageNames.push(filename)
//   messages[filename] = require(`./static/language/${filename}`)
// })
// Vue.prototype.$languageNames = languageNames

// const i18n = {
//   locale: cookie.get('lang') || 'ZH', // language,default 
//   messages: messages
// }


const i18n = new VueI18n({
 locale:cookie.get('lang') || 'EN', // language,default english
 messages: {
  'ZH': require('@/static/language/ZH.js'),
  'EN': require('@/static/language/EN.js')
 }
})

const app = new Vue({
    ...App,
	i18n,
	Base64,
	})
app.$mount()

const routeExists = (pathname, routes) => {
  for (let i = 0; i < routes.length; i++) {
    const route = routes[i];
    if ('#' + route.path === pathname) {
      return true;
    }
    if (route.path.includes(':')) {
      const regex = new RegExp('^' + route.path.replace(/:\w+/g, '\\w+').replace(/\//g, '\\/') + '$');
      if (regex.test(pathname)) {
        return true;
      }
    }
  }
  return false;
}

window.addEventListener('load', function () {
  const pathname = window.location.hash.split('?')[0];
  const exists = routeExists(pathname, app.$options.router.options.routes);

  if (!exists) {
    window.location.hash = '/';
  }
});

// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
import App from './App.vue'
export function createApp() {
  const app = createSSRApp(App)
  return {
    app
  }
}
// #endif